package com.hp15c.calculator;

import java.text.DecimalFormat;
import java.util.UUID;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainCalculator extends Activity
{
	private LayoutParams LAYOUTPARAMS;
	private ImageView imageUser, imageF, imageG, imageC, imageNeg, imageRad, imageGrad;	
	private LinearLayout layoutDisplayNumber;
	private ImageButton KEY_0, KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7, KEY_8, KEY_9;	
	private ImageButton KEY_DIV, KEY_X, KEY_EXCEPT, KEY_SUM, KEY_DOT, KEY_E;	
	private ImageButton KEY_CHS, KEY_EEX, KEY_ENTER;	
	private ImageButton KEY_SQRT_X, KEY_E_HAT_X, KEY_10_HAT_X, KEY_Y_HAT_X, KEY_1_DIV_X;	
	private ImageButton KEY_SST, KEY_GTO, KEY_SIN, KEY_COS, KEY_TAN;	
	private ImageButton KEY_R_DIV_S, KEY_GSB, KEY_R_DOWN, KEY_X_AND_Y, KEY_BACK;	
	private ImageButton KEY_ON, KEY_F, KEY_G, KEY_STO, KEY_RCL;
	
	private Bitmap[] NUMBER 			= null;
	private String SAVE_RESULTS	 		= "";
	private String SAVE_ENTER	 		= "";
	private String VALUE_DISPLAY 		= "";	
	private String VALUE_RESULTS	 	= "";
	private String VALUE_DEFAULT	 	= "0.0000";
	private String LAST_X = "";
	private String X = "";
	private String Y = "";
	private boolean USE_F		= false;
	private boolean USE_G		= false;
	private boolean USE_C		= false;
	private boolean USE_DOT		= false;
	private boolean USE_USER	= false;
	private boolean USE_EXCEPT	= false;
	private boolean USER_INPUT	= false;
	
	private final int ACTION_SUM 			= 1;
	private final int ACTION_EXCEPT 		= 2;
	private final int ACTION_X	 			= 3;
	private final int ACTION_DIV	 		= 4;
	private final int ACTION_SQRT	 		= 5;
	private final int ACTION_E_HAT_X 		= 6;	
	private final int ACTION_COS	 		= 7;
	private final int ACTION_SIN	 		= 8;
	private final int ACTION_TAN	 		= 9;
	private final int ACTION_10_HAT_X 		= 10;
	private final int ACTION_ABS_BLUE 		= 11;
	private final int ACTION_COS_BLUE 		= 12;
	private final int ACTION_SIN_BLUE 		= 13;
	private final int ACTION_TAN_BLUE 		= 14;
	private final int ACTION_E_HAT_X_BLUE 	= 15;
	private final int ACTION_10_HAT_X_BLUE 	= 16;
	private final int ACTION_SQRT_2_BLUE 	= 17;
	
	private CaculatorMethods calculatorMethods;
	private DecimalFormat df = new DecimalFormat(VALUE_DEFAULT);
	
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        init();
        NUMBER = createBitmapNumber();
        displayNumberImages(VALUE_DEFAULT);        
        calculatorMethods = new CaculatorMethods(this);
    }
    
    private View.OnClickListener clickListener = new OnClickListener()
    {
		@Override
		public void onClick(View key)
		{
			if(KEY_0 == key) {
				if(USE_G) {
					delG();
				} else if(USE_F) {
					delF();
				} else {
					X += "0";					
					displayNumber();
				}
			}
			else if(KEY_1 == key) {
				if(USE_G) {
					delG();
				} else if(USE_F) {
					delF();
				} else {
					X += "1";
					displayNumber();
				}
			}
			else if(KEY_2 == key) {
				if(USE_G) {
					delG();
				} else if(USE_F) {
					delF();
				} else {
					X += "2";
					displayNumber();
				}
			}
			else if(KEY_3 == key) {
				if(USE_G) {
					delG();
				} else if(USE_F) {
					delF();
				} else {
					X += "3";
					displayNumber();
				}
			}
			else if(KEY_4 == key) {
				if(USE_G) {
					delG();
				} else if(USE_F) {
					delF();
				} else {
					X += "4";
					displayNumber();
				}
			}
			else if(KEY_5 == key) {
				if(USE_G) {
					delG();
				} else if(USE_F) {
					delF();
				} else {
					X += "5";
					displayNumber();
				}
			}
			else if(KEY_6 == key) {
				if(USE_G) {
					delG();
				} else if(USE_F) {
					delF();
				} else {
					X += "6";
					displayNumber();
				}
			}
			else if(KEY_7 == key) {
				if(USE_G) {
					delG();
					imageRad.setVisibility(View.GONE);
					imageGrad.setVisibility(View.GONE);
				} else if(USE_F) {
					delF();
				} else {
					X += "7";
					displayNumber();
				}
			}
			else if(KEY_8 == key) {
				if(USE_G) {
					delG();
					imageGrad.setVisibility(View.GONE);
					imageRad.setVisibility(View.VISIBLE);					
				} else if(USE_F) {
					delF();
				} else {
					X += "8";
					displayNumber();
				}
			}
			else if(KEY_9 == key) {
				if(USE_G) {
					delG();
					imageRad.setVisibility(View.GONE);
					imageGrad.setVisibility(View.VISIBLE);
				} else if(USE_F) {
					delF();
				} else {
					X += "9";
					displayNumber();
				}
			}
			else if(KEY_SUM == key) {
				if(USE_G) {
					delG();
				} else if(USE_F) {
					delF();
				} else {
					if(checkInput2Value()) {
						calculator(ACTION_SUM);
					}
				}				
			}
			else if(KEY_EXCEPT == key)
			{
				if(USE_G) {
					delG();
				} else if(USE_F) {
					delF();
					if(!USE_C) {
						useC();
					}
				} else {
					USER_INPUT = false;
					if(X.length() == 0 && Y.length() == 0 && LAST_X.length() > 0) {
						LAST_X = "";
						return;
					} else {
						if(X.length() > 0 && Y.length() == 0) {
							Log.v("USE_EXCEPT", "218");
							if(USE_EXCEPT) {
								delExcept();
								X = LAST_X.replace("-", "");
								LAST_X = "-" + X;
							} else {
								useExcept();
								LAST_X = X;
								X = "-" + X;							
							}
							Y = X; X = "";
							VALUE_DISPLAY = df.format(Double.valueOf(Y)).replace("-", "");
							displayNumberImages(VALUE_DISPLAY);
						} else if(X.length() == 0 && Y.length() > 0) {
							Log.v("USE_EXCEPT", "232");
							if(USE_EXCEPT) {
								delExcept();
								LAST_X = Y;
								Y = Y.replace("-", "");
								VALUE_RESULTS = Y;
								VALUE_DISPLAY = df.format(Double.valueOf(Y));
							} else {
								useExcept();
								LAST_X = Y;
								Y = "-" + Y;
								VALUE_RESULTS = Y;
								VALUE_DISPLAY = df.format(Double.valueOf(LAST_X));
							}
							boolean character = true;
							String[] array = VALUE_RESULTS.split("-");
							if(array.length == 2) {
								character = false;
							}
							String formatE = DisplayNumber.formatE(VALUE_RESULTS, character);
							if(formatE == null) {
								VALUE_RESULTS = df.format(Double.valueOf(VALUE_RESULTS));
								VALUE_DISPLAY = DisplayNumber.format(VALUE_RESULTS);
							} else {
								VALUE_DISPLAY = formatE;
							}
							displayNumberImages(VALUE_DISPLAY);
						} else if(X.length() > 0 && Y.length() > 0) {
							if(checkInput2Value()) {
								calculator(ACTION_EXCEPT);
							}
						}
						Log.v("LAST_X", "LAST_X="+LAST_X);
					}
				}
			}
			else if(KEY_X == key) {
				if(USE_G) {
					delG();
				} else if(USE_F) {
					delF();
				} else {
					if(checkInput2Value()) {
						calculator(ACTION_X);
					}
				}
			}
			else if(KEY_DIV == key) {
				if(USE_G) {
					delG();
				} else if(USE_F) {
					delF();
				} else {
					if(checkInput2Value()) {
						calculator(ACTION_DIV);
					}
				}
			}
			else if(KEY_DOT == key) {
				if(USE_DOT) { return; }
				if(X.length() > 0) {
					if(X.length() > 10) {
						VALUE_DISPLAY = VALUE_DISPLAY.substring(0, 13);
						SAVE_RESULTS = VALUE_DISPLAY + ".";
					} else {
						SAVE_RESULTS = "";
					}
					X += ".";
					VALUE_DISPLAY += ".";					
					displayNumberImages(VALUE_DISPLAY);					
				} else {
					X += "0.";
					VALUE_DISPLAY = X;
					displayNumberImages(VALUE_DISPLAY);
				}
				USE_DOT = true;
			}
			else if(KEY_E == key)
			{
				if(USE_G) {
					delG();
				} else if(USE_F) {
					delF();
				} else {
					
				}
			}
			//---Row 1
			else if(KEY_SQRT_X == key)
			{
				if(USE_G) {
					delG();					
					if(checkInput1Value()) {
						calculator(ACTION_SQRT_2_BLUE);
					}
				} else if(USE_F) {
					delF();
				} else {
					if(checkInput1Value()) {
						calculator(ACTION_SQRT);
					}
				}
			}
			else if(KEY_E_HAT_X == key)
			{
				if(USE_G) {
					delG();
					if(checkInput1Value()) {
						calculator(ACTION_E_HAT_X_BLUE);
					}
				} else if(USE_F) {
					delF();
				} else {
					if(checkInput1Value()) {
						calculator(ACTION_E_HAT_X);
					}
				}
			}
			else if(KEY_10_HAT_X == key)
			{
				if(USE_G) {
					delG();					
					if(checkInput1Value()) {
						calculator(ACTION_10_HAT_X_BLUE);
					}
				} else if(USE_F) {
					delF();
				} else {
					if(checkInput2Value()) {
						calculator(ACTION_10_HAT_X);
					}
				}
			}
			else if(KEY_Y_HAT_X == key)
			{
				if(USE_G) {
					delG();
				} else if(USE_F) {
					delF();
				} else {
					
				}
			}
			else if(KEY_1_DIV_X == key)
			{
				if(USE_G) {
					delG();
				} else if(USE_F) {
					delF();
				} else {
					
				}
			}
			else if(KEY_CHS == key)
			{
				if(USE_G) {
					delG();
					if(checkInput1Value()) {
						calculator(ACTION_ABS_BLUE);
					}
				} else if(USE_F) {
					delF();
				} else {
					
				}
			}
			//---Row 2
			else if(KEY_SST == key)
			{
				if(USE_G) {
					delG();
				} else if(USE_F) {
					delF();
				} else {
					
				}
			}
			else if(KEY_GTO == key)
			{
				if(USE_G) {
					delG();
				} else if(USE_F) {
					delF();
				} else {
					
				}
			}
			else if(KEY_SIN == key)
			{
				if(USE_G) {
					delG();
					if(checkInput1Value()) {
						calculator(ACTION_SIN_BLUE);
					}
				} else if(USE_F) {
					delF();
				} else {
					if(checkInput1Value()) {
						calculator(ACTION_SIN);
					}
				}
			}
			else if(KEY_COS == key)
			{
				if(USE_G) {
					delG();
					if(checkInput1Value()) {
						calculator(ACTION_COS_BLUE);
					}
				} else if(USE_F) {
					delF();
				} else {
					if(checkInput1Value()) {
						calculator(ACTION_COS);
					}
				}
			}			
			else if(KEY_TAN == key)
			{
				if(USE_G) {
					delG();
					if(checkInput1Value()) {
						calculator(ACTION_TAN_BLUE);
					}
				} else if(USE_F) {
					delF();
				} else {
					if(checkInput1Value()) {
						calculator(ACTION_TAN);
					}
				}				
			}
			else if(KEY_EEX == key)
			{
				if(USE_G) {
					delG();
				} else if(USE_F) {
					delF();
				} else {
					
				}
			}
			//---Row 3
			else if(KEY_R_DIV_S == key)
			{
				if(USE_G) {
					delG();
				} else if(USE_F) {
					delF();
				} else {
					
				}
			}
			else if(KEY_GSB == key)
			{
				if(USE_G) {
					delG();
				} else if(USE_F) {
					delF();
				} else {
					
				}
			}
			else if(KEY_R_DOWN == key)
			{
				if(USE_G) {
					delG();
				} else if(USE_F) {
					delF();
				} else {
					
				}
			}
			else if(KEY_X_AND_Y == key)
			{
				if(USE_G) {
					delG();
				} else if(USE_F) {
					delF();
				} else {
					
				}
			}
			else if(KEY_BACK == key) {				
				if(USER_INPUT) {
					if(X.length() > 0) {
						if(X.substring(X.length()-1, X.length()).equals(".")) {
							USE_DOT = false;
						}
						X = X.substring(0, X.length()-1);						
						if(X.length() > 0) {
							if(X.length() > 10 && SAVE_RESULTS.length() > 0) {
								displayNumberImages(SAVE_RESULTS);
							} else {
								SAVE_RESULTS = "";
								VALUE_DISPLAY = DisplayNumber.format(X).replace(":", ".");
								displayNumberImages(VALUE_DISPLAY);
							}
						} else {
							displayNumberImages(VALUE_DEFAULT);
						}
					} else {
						USER_INPUT = false;
					}
				} else {
					X = ""; Y = "";
					VALUE_DISPLAY = "";
					VALUE_RESULTS = "";
					displayNumberImages(VALUE_DEFAULT);
					imageNeg.setVisibility(View.GONE);
				}
			}
			//---Row 4
			else if(KEY_ON == key) {
				clearAllProperties();
			}			
			else if(KEY_F == key) {
				useF();
				delG();
			}
			else if(KEY_G == key) {
				useG();
				delF();
			}
			else if(KEY_STO == key)
			{
				if(USE_G) {
					delG();
				} else if(USE_F) {
					delF();
				} else {
					
				}
			}
			else if(KEY_RCL == key)
			{
				if(USE_G) {
					delG();
				} else if(USE_F) {
					delF(); 
					if(USE_USER) {
						delUser();					
					} else {
						useUser();
					}					
				} else {
					
				}
			}
			else if(KEY_ENTER == key) {
				if(USE_G) {
					delG();
					Log.v("LAST_X", "LAST_X="+LAST_X);
					if(LAST_X.length() > 0) {
						Y = LAST_X;
						if(Double.valueOf(Y) > 0) {
							imageNeg.setVisibility(View.GONE);
						} else {
							imageNeg.setVisibility(View.VISIBLE);
						}
						VALUE_DISPLAY = df.format(Double.valueOf(Y)).replace("-", "");
						displayNumberImages(VALUE_DISPLAY);
					}
					USER_INPUT = true;
				}
				else if(USE_F) {
					delF();
					Y = String.valueOf(Math.random());
					VALUE_DISPLAY = df.format(Double.valueOf(Y)).replace("-", "");
					displayNumberImages(VALUE_DISPLAY);
					USER_INPUT = true;
				}
				else {
					if(X.length() > 0 && Y.length() == 0) { Y = X; }
					if(X.length() == 0 && Y.length() > 0) { SAVE_ENTER = Y; }
					if(Y.length() > 0)
					{
						VALUE_DISPLAY = df.format(Double.valueOf(Y)).replace("-", "");
						displayNumberImages(VALUE_DISPLAY);
						USER_INPUT = true;
						Log.v("X", "X="+X);
						Log.v("Y", "Y="+Y);
						Log.v("Y", "E="+SAVE_ENTER);
					}
				}
			}
		}
	};
	
	private void useF() {
		USE_F = true;
		imageF.setVisibility(View.VISIBLE);
	}
	
	private void delF() {
		USE_F = false;
		imageF.setVisibility(View.GONE);
	}
	
	private void useG() {
		USE_G = true;
		imageG.setVisibility(View.VISIBLE);	
	}
	
	private void delG() {
		USE_G = false;
		imageG.setVisibility(View.GONE);
	}
	
	private void useC() {
		USE_C = true;
		imageC.setVisibility(View.VISIBLE);
	}
	
	private void useUser() {
		USE_USER = true;						
		imageUser.setVisibility(View.VISIBLE);
	}
	
	private void delUser() {
		USE_USER = false;
		imageUser.setVisibility(View.GONE);
	}
	
	private void useExcept() {
		USE_EXCEPT = true;
		imageNeg.setVisibility(View.VISIBLE);
	}
	
	private void delExcept() {
		USE_EXCEPT = false;
		imageNeg.setVisibility(View.GONE);
	}
	
	public boolean checkInput1Value()
	{
		USE_DOT = false;
		if(SAVE_ENTER.length() > 0) { 
			X = SAVE_ENTER;
			SAVE_ENTER= "";
		};
		if(X.length() >= 0)
		{
			LAST_X = X;
			VALUE_DISPLAY = X;
			displayNumberImages(VALUE_DISPLAY);
			return true;
		}
		return false;
	}
	
	public boolean checkInput2Value()
	{
		USE_DOT = false;
		if(SAVE_ENTER.length() > 0) { 
			X = SAVE_ENTER;
			SAVE_ENTER= "";
		};
		if(X.length() >= 0 && Y.length() == 0)
		{
			Y = X;
			LAST_X = X;
			VALUE_DISPLAY = X;
			X = "";
			VALUE_DISPLAY = df.format(Double.valueOf(VALUE_DISPLAY));
			VALUE_DISPLAY = DisplayNumber.format(VALUE_DISPLAY);
			displayNumberImages(VALUE_DISPLAY);
		}
		else if(X.length() > 0 && Y.length() > 0)
		{
			return true;
		}
		return false;
	}
	
	private void calculator(int action)
	{
		boolean character = true;
		double x = 0, y = 0;
		if(X.length() > 0)
		{
			x = Double.valueOf(X);
		}
		if(Y.length() > 0)
		{
			y = Double.valueOf(Y);		
		}
		switch(action)
		{
		case ACTION_SUM:
			VALUE_RESULTS = String.valueOf(calculatorMethods.sum(x, y));
			break;
		case ACTION_EXCEPT:
			VALUE_RESULTS = String.valueOf(calculatorMethods.except(y, x));
			break;	
		case ACTION_X:
			VALUE_RESULTS = String.valueOf(calculatorMethods.x(x, y));
			break;
		case ACTION_DIV:
			VALUE_RESULTS = String.valueOf(calculatorMethods.div(y, x));
			break;
		case ACTION_E_HAT_X:
			if(X.length() > 0) {						
				VALUE_RESULTS = String.valueOf(calculatorMethods.exp(x));
			} else {
				if(Y.length() > 0) {
					VALUE_RESULTS = String.valueOf(calculatorMethods.exp(y));					
				}
			}
			break;
		case ACTION_SQRT:
			if(X.length() > 0) {						
				VALUE_RESULTS = String.valueOf(calculatorMethods.sqrt(x));
			} else {
				if(Y.length() > 0) {
					VALUE_RESULTS = String.valueOf(calculatorMethods.sqrt(y));					
				}
			}
			break;
		case ACTION_SQRT_2_BLUE:
			if(X.length() > 0) {						
				VALUE_RESULTS = String.valueOf(calculatorMethods.pow2(x));
			} else {
				if(Y.length() > 0) {
					VALUE_RESULTS = String.valueOf(calculatorMethods.pow2(y));					
				}
			}
			break;
		case ACTION_E_HAT_X_BLUE:
			if(X.length() > 0) {						
				VALUE_RESULTS = String.valueOf(calculatorMethods.log(x));
			} else {
				if(Y.length() > 0) {
					VALUE_RESULTS = String.valueOf(calculatorMethods.log(y));					
				}
			}
			break;
		case ACTION_10_HAT_X:
			VALUE_RESULTS = String.valueOf(calculatorMethods.powy(y, x));
			break;
		case ACTION_10_HAT_X_BLUE:
			if(X.length() > 0) {						
				VALUE_RESULTS = String.valueOf(calculatorMethods.log10(x));
			} else {
				if(Y.length() > 0) {
					VALUE_RESULTS = String.valueOf(calculatorMethods.log10(y));					
				}
			}
			break;
		case ACTION_ABS_BLUE:
			if(X.length() > 0) {						
				VALUE_RESULTS = String.valueOf(calculatorMethods.abs(x));
			} else {
				if(Y.length() > 0) {
					VALUE_RESULTS = String.valueOf(calculatorMethods.abs(y));					
				}
			}
			break;
		case ACTION_COS:
			character = false;
			if(X.length() > 0) {						
				VALUE_RESULTS = String.valueOf(calculatorMethods.cos(x));
			} else {
				if(Y.length() > 0) {
					VALUE_RESULTS = String.valueOf(calculatorMethods.cos(y));					
				}
			}
			break;
		case ACTION_COS_BLUE:
			character = false;
			if(X.length() > 0) {						
				VALUE_RESULTS = String.valueOf(calculatorMethods.acos(x));
			} else {
				if(Y.length() > 0) {
					VALUE_RESULTS = String.valueOf(calculatorMethods.acos(y));					
				}
			}
			break;
		case ACTION_SIN:
			character = false;
			if(X.length() > 0) {						
				VALUE_RESULTS = String.valueOf(calculatorMethods.sin(x));
			} else {
				if(Y.length() > 0) {
					VALUE_RESULTS = String.valueOf(calculatorMethods.sin(y));					
				}
			}
			break;
		case ACTION_SIN_BLUE:
			character = false;
			if(X.length() > 0) {						
				VALUE_RESULTS = String.valueOf(calculatorMethods.asin(x));
			} else {
				if(Y.length() > 0) {
					VALUE_RESULTS = String.valueOf(calculatorMethods.asin(y));					
				}
			}
			break;
		case ACTION_TAN:
			character = false;
			if(X.length() > 0) {						
				VALUE_RESULTS = String.valueOf(calculatorMethods.tan(x));
			} else {
				if(Y.length() > 0) {
					VALUE_RESULTS = String.valueOf(calculatorMethods.tan(y));					
				}
			}
			break;
		case ACTION_TAN_BLUE:
			character = false;
			if(X.length() > 0) {						
				VALUE_RESULTS = String.valueOf(calculatorMethods.atan(x));
			} else {
				if(Y.length() > 0) {
					VALUE_RESULTS = String.valueOf(calculatorMethods.atan(y));					
				}
			}
			break;
		default:
			break;
		}
		USER_INPUT = false;
		if(VALUE_RESULTS.length() > 0)
		{
			if(Double.valueOf(VALUE_RESULTS) > 0) {
				imageNeg.setVisibility(View.GONE);
			} else if(VALUE_RESULTS.trim().equals("0.0")) {
				imageNeg.setVisibility(View.GONE);
			} else {
				imageNeg.setVisibility(View.VISIBLE);
			}
			Y = VALUE_RESULTS;
			Log.v("VALUE_RESULTS", "VALUE_RESULTS="+VALUE_RESULTS);
			String formatE = DisplayNumber.formatE(VALUE_RESULTS, character);
			if(formatE == null)
			{
				VALUE_RESULTS = df.format(Double.valueOf(VALUE_RESULTS));
				VALUE_DISPLAY = DisplayNumber.format(VALUE_RESULTS);
			}
			else if(formatE.trim().equals("Error"))
			{
				VALUE_DISPLAY = formatE;
			}
			else
			{
				VALUE_DISPLAY = formatE;
			}
			displayNumberImages(VALUE_DISPLAY);
			LAST_X = X; X = "";
		}
	}
	
	private void displayNumber()
    {
		SAVE_ENTER = "";
		USER_INPUT = true;
		imageNeg.setVisibility(View.GONE);
		if(X.length() > 10 && SAVE_RESULTS.length() > 0) {
			displayNumberImages(SAVE_RESULTS);
		} else {
			VALUE_DISPLAY = DisplayNumber.format(X);
		}
    	displayNumberImages(VALUE_DISPLAY);
    }
	
	private void displayNumberImages(String number)
	{
		layoutDisplayNumber.removeAllViewsInLayout();
		int i = 0, j = 0;
		String num = "";		
		for(i = 0; i < number.length(); i++)
		{
			j++;
			num = number.toString().trim().substring(i, j);
			ImageView image = new ImageView(getApplicationContext());
			image.setLayoutParams(LAYOUTPARAMS);
			if(num.trim().equals(","))
			{
				image.setImageBitmap(NUMBER[10]);
			}
			else if(num.trim().equals("."))
			{
				image.setImageBitmap(NUMBER[11]);
			}
			else if(num.trim().equals("-"))
			{
				image.setImageBitmap(NUMBER[12]);
			}
			else if(num.trim().equals("E"))
			{
				image.setImageBitmap(NUMBER[13]);
			}
			else if(num.trim().equals("N"))
			{
				image.setImageBitmap(NUMBER[14]);
			}
			else if(num.trim().equals("a"))
			{
				image.setImageBitmap(NUMBER[15]);
			}
			else if(num.trim().equals("W"))
			{
				image.setImageBitmap(NUMBER[16]);
			}
			else if(num.trim().equals("o"))
			{
				image.setImageBitmap(NUMBER[17]);
			}
			else if(num.trim().equals("r"))
			{
				image.setImageBitmap(NUMBER[18]);
			}
			else
			{
				image.setImageBitmap(NUMBER[Integer.valueOf(num)]);
			}
			layoutDisplayNumber.addView(image);
		}
		layoutDisplayNumber.invalidate();
	}
	
	private Bitmap[] createBitmapNumber()
	{
		Bitmap[] bitmap = new Bitmap[19];
		bitmap[0] = BitmapFactory.decodeResource(getResources(), R.drawable.number_0);
		bitmap[1] = BitmapFactory.decodeResource(getResources(), R.drawable.number_1);
		bitmap[2] = BitmapFactory.decodeResource(getResources(), R.drawable.number_2);
		bitmap[3] = BitmapFactory.decodeResource(getResources(), R.drawable.number_3);
		bitmap[4] = BitmapFactory.decodeResource(getResources(), R.drawable.number_4);
		bitmap[5] = BitmapFactory.decodeResource(getResources(), R.drawable.number_5);
		bitmap[6] = BitmapFactory.decodeResource(getResources(), R.drawable.number_6);
		bitmap[7] = BitmapFactory.decodeResource(getResources(), R.drawable.number_7);
		bitmap[8] = BitmapFactory.decodeResource(getResources(), R.drawable.number_8);
		bitmap[9] = BitmapFactory.decodeResource(getResources(), R.drawable.number_9);
		bitmap[10] = BitmapFactory.decodeResource(getResources(), R.drawable.comma);
		bitmap[11] = BitmapFactory.decodeResource(getResources(), R.drawable.decimal);
		bitmap[12] = BitmapFactory.decodeResource(getResources(), R.drawable.neg);
		bitmap[13] = BitmapFactory.decodeResource(getResources(), R.drawable.e);
		bitmap[14] = BitmapFactory.decodeResource(getResources(), R.drawable.n);
		bitmap[15] = BitmapFactory.decodeResource(getResources(), R.drawable.a);
		bitmap[16] = BitmapFactory.decodeResource(getResources(), R.drawable.number_empty);
		bitmap[17] = BitmapFactory.decodeResource(getResources(), R.drawable.oo);
		bitmap[18] = BitmapFactory.decodeResource(getResources(), R.drawable.rr);
		return bitmap;
	}
	
	private void init()
    {
		LAYOUTPARAMS = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		layoutDisplayNumber = (LinearLayout)findViewById(R.id.display_number);
		imageF 		= (ImageView)findViewById(R.id.image_f);
		imageG 		= (ImageView)findViewById(R.id.image_g);
		imageC		= (ImageView)findViewById(R.id.image_c);
		imageNeg 	= (ImageView)findViewById(R.id.image_neg);
		imageUser 	= (ImageView)findViewById(R.id.image_user);
		imageRad 	= (ImageView)findViewById(R.id.image_rad);
		imageGrad 	= (ImageView)findViewById(R.id.image_grad);
		
		
		imageF.setVisibility(View.GONE);
		imageG.setVisibility(View.GONE);
		imageC.setVisibility(View.GONE);
		imageUser.setVisibility(View.GONE);
		imageNeg.setVisibility(View.GONE);
		imageRad.setVisibility(View.GONE);
		imageGrad.setVisibility(View.GONE);		
		
    	KEY_0 = (ImageButton)findViewById(R.id.key_0);
    	KEY_1 = (ImageButton)findViewById(R.id.key_1);
    	KEY_2 = (ImageButton)findViewById(R.id.key_2);
    	KEY_3 = (ImageButton)findViewById(R.id.key_3);
    	KEY_4 = (ImageButton)findViewById(R.id.key_4);
    	KEY_5 = (ImageButton)findViewById(R.id.key_5);
    	KEY_6 = (ImageButton)findViewById(R.id.key_6);
    	KEY_7 = (ImageButton)findViewById(R.id.key_7);
    	KEY_8 = (ImageButton)findViewById(R.id.key_8);
    	KEY_9 = (ImageButton)findViewById(R.id.key_9);    	
    	KEY_0.setOnClickListener(clickListener);
    	KEY_1.setOnClickListener(clickListener);
    	KEY_2.setOnClickListener(clickListener);
    	KEY_3.setOnClickListener(clickListener);
    	KEY_4.setOnClickListener(clickListener);
    	KEY_5.setOnClickListener(clickListener);
    	KEY_6.setOnClickListener(clickListener);
    	KEY_7.setOnClickListener(clickListener);
    	KEY_8.setOnClickListener(clickListener);
    	KEY_9.setOnClickListener(clickListener);
    	
    	KEY_DIV 	= (ImageButton)findViewById(R.id.key_div);
    	KEY_X 		= (ImageButton)findViewById(R.id.key_x);
    	KEY_EXCEPT 	= (ImageButton)findViewById(R.id.key_except);
    	KEY_SUM 	= (ImageButton)findViewById(R.id.key_add);
    	KEY_DOT 	= (ImageButton)findViewById(R.id.key_dot);
    	KEY_E 		= (ImageButton)findViewById(R.id.key_e);    	
    	KEY_DIV.setOnClickListener(clickListener);
    	KEY_X.setOnClickListener(clickListener);
    	KEY_EXCEPT.setOnClickListener(clickListener);
    	KEY_SUM.setOnClickListener(clickListener);
    	KEY_DOT.setOnClickListener(clickListener);
    	KEY_E.setOnClickListener(clickListener);
    	
    	KEY_CHS 	= (ImageButton)findViewById(R.id.key_CHS);
    	KEY_EEX 	= (ImageButton)findViewById(R.id.key_EEX);
    	KEY_ENTER	= (ImageButton)findViewById(R.id.key_Enter);    	
    	KEY_CHS.setOnClickListener(clickListener);
    	KEY_EEX.setOnClickListener(clickListener);
    	KEY_ENTER.setOnClickListener(clickListener);
    	
    	KEY_SQRT_X		= (ImageButton)findViewById(R.id.key_sqrt_x);
    	KEY_E_HAT_X		= (ImageButton)findViewById(R.id.key_ex);
    	KEY_10_HAT_X	= (ImageButton)findViewById(R.id.key_10x);
    	KEY_Y_HAT_X		= (ImageButton)findViewById(R.id.key_yx);
    	KEY_1_DIV_X		= (ImageButton)findViewById(R.id.key_1x);    	
    	KEY_SQRT_X.setOnClickListener(clickListener);
    	KEY_E_HAT_X.setOnClickListener(clickListener);
    	KEY_10_HAT_X.setOnClickListener(clickListener);
    	KEY_Y_HAT_X.setOnClickListener(clickListener);
    	KEY_1_DIV_X.setOnClickListener(clickListener);
    	
    	KEY_SST	= (ImageButton)findViewById(R.id.key_SST);
    	KEY_GTO	= (ImageButton)findViewById(R.id.key_GTO);
    	KEY_SIN	= (ImageButton)findViewById(R.id.key_SIN);
    	KEY_COS	= (ImageButton)findViewById(R.id.key_COS);
    	KEY_TAN	= (ImageButton)findViewById(R.id.key_TAN);    	
    	KEY_SST.setOnClickListener(clickListener);
    	KEY_GTO.setOnClickListener(clickListener);
    	KEY_SIN.setOnClickListener(clickListener);
    	KEY_COS.setOnClickListener(clickListener);
    	KEY_TAN.setOnClickListener(clickListener);
    	
    	KEY_R_DIV_S	= (ImageButton)findViewById(R.id.key_RS);
    	KEY_GSB		= (ImageButton)findViewById(R.id.key_GSB);
    	KEY_R_DOWN	= (ImageButton)findViewById(R.id.key_R);
    	KEY_X_AND_Y	= (ImageButton)findViewById(R.id.key_xy);
    	KEY_BACK	= (ImageButton)findViewById(R.id.key_back);
    	KEY_R_DIV_S.setOnClickListener(clickListener);
    	KEY_GSB.setOnClickListener(clickListener);
    	KEY_R_DOWN.setOnClickListener(clickListener);
    	KEY_X_AND_Y.setOnClickListener(clickListener);
    	KEY_BACK.setOnClickListener(clickListener);    	
    	
    	KEY_ON	= (ImageButton)findViewById(R.id.key_ON);
    	KEY_F	= (ImageButton)findViewById(R.id.key_f);
    	KEY_G	= (ImageButton)findViewById(R.id.key_g);
    	KEY_STO	= (ImageButton)findViewById(R.id.key_STO);
    	KEY_RCL	= (ImageButton)findViewById(R.id.key_RCL);
    	KEY_ON.setOnClickListener(clickListener);
    	KEY_F.setOnClickListener(clickListener);
    	KEY_G.setOnClickListener(clickListener);
    	KEY_STO.setOnClickListener(clickListener);
    	KEY_RCL.setOnClickListener(clickListener);   
    }
	
	private void clearAllProperties()
	{
		imageC.setVisibility(View.GONE);
		imageF.setVisibility(View.GONE);
		imageG.setVisibility(View.GONE);
		imageGrad.setVisibility(View.GONE);
		imageNeg.setVisibility(View.GONE);
		imageRad.setVisibility(View.GONE);
		imageUser.setVisibility(View.GONE);
		USE_C		= false;
		USE_F		= false;
		USE_G		= false;
		USE_DOT		= false;
		USE_USER	= false;
		USE_EXCEPT	= false;
		USER_INPUT	= false;
		VALUE_DISPLAY = "";
		VALUE_RESULTS = "";
		LAST_X = ""; X = ""; Y = "";
		displayNumberImages(VALUE_DEFAULT);
	}
	
	@Override
	protected void onDestroy()
	{
		super.onDestroy();
		if(NUMBER != null)
		{
			NUMBER = null;
		}
		System.gc();
	}
}